package com.atguigu.sink;

import com.atguigu.bean.WaterSensor;
import com.atguigu.datasource.RandomWatersensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;


public class Flink01_sinkKafka {
    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port",10000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(configuration);
        env.setParallelism(2);

        DataStreamSource<WaterSensor> ds = env.addSource(new RandomWatersensor());

        //向kafka中写入数据
        /*
        参数解读：String brokerList：kafka集群地址
        String topicId：主题名称
        SerializationSchema<IN> serializationSchema：序列化机制，传递什么类型的数据，就用什么类型的序列化机制
         */

        ds.map(new MapFunction<WaterSensor, String>() {
            @Override
            public String map(WaterSensor value) throws Exception {
                return value.toString();
            }
        }).addSink(new FlinkKafkaProducer<String>("hadoop102:9092", "sensor",new SimpleStringSchema()));


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
